package org.ands.input;

import java.util.ArrayList;
import java.util.List;

import javax.xml.namespace.NamespaceContext;

import org.ands.xml.DOMHelperCloneRoot;
import org.ands.xml.DOMHelperFromFile;
import org.ands.xml.NamespaceContextMCP;
import org.ands.xml.UniversalNamespaceResolver;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class InputHelperFactoryNodeList	implements IInputHelperFactory {


	public InputHelperFactoryNodeList(NodeList nodeList, NamespaceContext nsContext) throws Exception{
		init(nodeList, nsContext);
	}
	
	public InputHelperFactoryNodeList(NodeList nodeList) throws Exception{
		init(nodeList, null);
	}
	
	private void init(NodeList nodeList, NamespaceContext nsContext) throws Exception{
		assert(nodeList != null);
		if(nodeList == null){
			throw new Exception("Requires populated object list");
		}
		
		_nodeList = nodeList;
		
		_nsContext = nsContext;
		
		_nextIndex = 0;
	}
	
	public boolean hasNextInputHelper(){
		return (_nextIndex < _nodeList.getLength());
	}
	
	@Override 
	public IInputHelper getNextInputHelper() throws Exception {
		
		int currentIndex = _nextIndex;
		_nextIndex++; 
		
		assert(currentIndex < _nodeList.getLength());
		
		if(_nsContext != null)
			return new DOMHelperCloneRoot((Element)_nodeList.item(currentIndex), _nsContext, true /* deep */);
		
		return new DOMHelperCloneRoot((Element)_nodeList.item(currentIndex), true /* deep */);
	}
	
	NodeList _nodeList;
	NamespaceContext _nsContext;
	int _nextIndex;
	
};